import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:lib_util/ByteUtils.dart';
import 'package:image/image.dart' as img;
import 'package:ileddemo/code/ble/protocol/base/SourceInterface.dart';

import 'package:ileddemo/code/ble/protocol/base/RectConverter.dart';
import 'package:ileddemo/code/ble/protocol/iledcolor/source/partition/PartitionSourceBean.dart';

import '../../base/SourceType.dart';
import '../BleBean.dart';
import 'gif/GifDecoder.dart';

///iledcolor协议的素材数据源接口
abstract class ColorSourceInterface extends SourceInterface {
  //特效
  int effects = 0;

  //速度
  int speed = 1;

  //边框类型
  int frameType = 0;

  //亮度
  int brightness = 100;

  //停留时间
  int dwellTime = 0;

  //总帧数
  int frameCount = 1;

  //素材尺寸
  @RectConverter()
  Rect rect = Rect.zero;

  //获取用于发送的数据
  Future<List<int>> getSendData(BleBean bleBean);

  //节目属性数据
  List<int> getProgramParameters(BleBean bleBean) {
    List<int> data = [];
    //分区起点
    data.addAll([0, 0, 0, 0]);
    //分区大小
    data.addAll(ByteUtils.shortToBytes(screenSize.width.toInt()));
    data.addAll(ByteUtils.shortToBytes(screenSize.height.toInt()));
    //预留
    data.addAll([0, 0, 0]);
    //节目类型
    data.add(sourceType);
    //总帧数
    data.addAll(ByteUtils.shortToBytes(frameCount));
    //特效
    data.add(effects);
    //速度
    data.add(speed);
    //停留时间
    if (effects == 0) {
      data.add(max(dwellTime, 30));
    } else {
      data.add(dwellTime);
    }

    //边框
    data.add(frameType);
    //亮度
    data.add(brightness);
    //预留
    data.addAll([0, 0, 0]);
    return data;
  }

  ///计算平均灰度
  int getAvgGray(img.Image image) {

    return GifDecoder.getAvgGray(image);
  }

  PartitionSourceBean castPartitionSourceBean() {
    PartitionSourceBean partitionSourceBean = PartitionSourceBean();
    partitionSourceBean.ids.add(id);
    partitionSourceBean.sources.add(this);
    partitionSourceBean.screenSize = screenSize;
    return partitionSourceBean;
  }
}
